//
//  CPCycleView.swift
//  cockpit
//
//  Created by Haijun Si on 2020/7/8.
//  Copyright © 2020 chenganrt. All rights reserved.
//


import UIKit
import FSPagerView
import Kingfisher

class CPCycleView: UICollectionViewCell {
    
    /// 图片链接完整地址 数组
//    var liveBanner : [String]? {
//        didSet {
//            self.pagerView.reloadData()
//            self.pagerControl.numberOfPages = liveBanner?.count ?? 0
//        }
//    }
    
    /// 图片名称 数组
    var  bannerImgs : [CPHomeCycleModel]? {
        didSet {
            self.pagerView.reloadData()
//            self.pagerControl.numberOfPages = bannerImgs?.count ?? 0
        }
    }
    
    // - 懒加载滚动图片浏览器
    private lazy var pagerView: FSPagerView = {
        let pagerView = FSPagerView()
        pagerView.delegate = self
        pagerView.dataSource = self
        pagerView.automaticSlidingInterval = 3
        pagerView.isInfinite = !pagerView.isInfinite
        
        pagerView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "CPCycleView")
        return pagerView
    }()
    
//    lazy var pagerControl:FSPageControl = {
//        let pageControl = FSPageControl()
//
//        //设置下标的个数
//        pageControl.numberOfPages = 8
//        //设置下标位置
//        pageControl.contentHorizontalAlignment = .center
//        //设置下标指示器边框颜色（选中状态和普通状态）
//        pageControl.setStrokeColor(.white, for: .normal)
//        pageControl.setStrokeColor(.white, for: .selected)
//        //设置下标指示器颜色（选中状态和普通状态）
////        pageControl.setFillColor(.clear, for: .normal)
//        pageControl.setFillColor(.white, for: .selected)
//        //设置下标指示器图片（选中状态和普通状态）
//        //pageControl.setImage(UIImage.init(named: "1"), for: .normal)
//        //pageControl.setImage(UIImage.init(named: "2"), for: .selected)
//        //绘制下标指示器的形状 (roundedRect绘制绘制圆角或者圆形)
//        pageControl.setPath(UIBezierPath.init(roundedRect: CGRect.init(x: 0, y: 0, width: 5, height: 5), cornerRadius: 4.0), for: .normal)
////        pageControl.setPath(UIBezierPath(rect: CGRect(x: 0, y: 0, width: 5, height: 5)), for: .normal)
//        pageControl.setPath(UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 5, height: 5)), for: .selected)
//        return pageControl
//
//    }()

//    var bannerList:[String]? {
//        didSet {
//            guard let model = bannerList else { return }
//            self.liveBanner = model
//            self.pagerView.reloadData()
//            self.pagerControl.numberOfPages = bannerList?.count
//        }
//    }
    
//    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
//        super.init(style: style, reuseIdentifier: reuseIdentifier)
//        self.addSubview(self.pagerView)
//        self.pagerView.snp.makeConstraints { (make) in
//            make.left.top.right.equalToSuperview()
//            make.height.equalToSuperview()
//        }
//    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.pagerView)
        self.pagerView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalToSuperview()
        }
        
//        self.addSubview(self.pagerControl)
//        self.pagerControl.snp.makeConstraints { (make) in
//            make.centerX.equalTo(self.pagerView)
//            make.bottom.equalTo(-15)
//            make.width.equalToSuperview()
//        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
}

extension CPCycleView: FSPagerViewDelegate, FSPagerViewDataSource {
    func numberOfItems(in pagerView: FSPagerView) -> Int {
//        return self.liveBanner?.count ??
        return self.bannerImgs?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "CPCycleView", at: index)
        
        if let modelArr = self.bannerImgs {
            let model = modelArr[index]
            if let imageUrl = model.imgUrl {
                cell.imageView?.kf.setImage(with: URL(string: imageUrl), placeholder:UIImage.init(named: DEFAULT_IMAGE), options: nil, progressBlock: nil, completionHandler: nil)
            }
        }
        
//        if self.liveBanner?.isEmpty ?? true {
//            let model = self.bannerImgs![index]
//            if let imageUrl = model.imgUrl {
//                cell.imageView?.setImage(url: imageUrl)
//            }
//        }
//        else {
//            cell.imageView?.kf.setImage(with: URL(string: (self.liveBanner?[index])!))
//        }

        return cell
    }
    
    
//    func pagerView(_ pagerView: FSPagerView, willDisplay cell: FSPagerViewCell, forItemAt index: Int) {
//        self.pagerControl.currentPage = index
//    }
    
}
